package source.binary;

public class Binary {
    /**
     * 二进制相加
     *
     * @param a
     * @param b
     * @return
     */
    public String addBinary(String a, String b) {
        StringBuilder sb = new StringBuilder();
        int sum = 0;
        for (int i = a.length() - 1, j = b.length() - 1; i >= 0 || j >= 0; i--, j--) {
            sum += ((i >= 0 ? a.charAt(i) - '0' : 0) + ((j >= 0) ? b.charAt(j) - '0' : 0));
            sb.append(sum % 2);
            sum /= 2;
        }
        sb.append(sum == 1 ? "1" : "");
        return sb.reverse().toString();
    }
}
